﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Globalization;
using System.Linq;
using System.Windows.Forms;
using LoU.Dungeons;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            cbUnitType1.DataSource = Enum.GetValues(typeof(UnitType));
            cbDungeonType.DataSource = Enum.GetValues(typeof(DungeonType));

            cbUnitType1.SelectedIndexChanged += cbUnitType_SelectedIndexChanged;
            cbUnitType2.SelectedIndexChanged += cbUnitType_SelectedIndexChanged;
            cbUnitType1.SelectedIndexChanged += cbUnitType1_SelectedIndexChanged;

            cbUnitType1.SelectedIndex = 0;
            UpdateUnit2Combo();
            UpdateBuildCombo();

            cbDungeonType.SelectedIndex = 0;
            
            dgv1.AutoGenerateColumns = false;
            dgv1.Columns.Add(new DataGridViewTextBoxColumn { Name = "dgvcRatioTroop1", DataPropertyName = "Ratio", HeaderText = "% troop 1" });
            dgv1.Columns.Add(new DataGridViewTextBoxColumn { Name = "dgvcGainPerTS", DataPropertyName = "TotalWithoutGoldPerTS", HeaderText = "Gain / TS", Width = 80 });
            dgv1.Columns.Add(new DataGridViewTextBoxColumn { Name = "dgvcMaxTS", DataPropertyName = "MaxReplaceableTSView", HeaderText = "Max replaceable TS", Width = 70 });
            dgv1.Columns.Add(new DataGridViewTextBoxColumn { Name = "dgvcBuild", DataPropertyName = "Build", HeaderText = "Build", MinimumWidth = 300 });
            dgv1.Columns.Add(new DataGridViewTextBoxColumn { Name = "dgvcGainPerHour", DataPropertyName = "TotalWithoutGoldPerHour", HeaderText = "Total without gold\n k / Hour" });
            dgv1.Columns.Add(new DataGridViewTextBoxColumn { Name = "dgvcFoodBalancePerHour", DataPropertyName = "FoodPerHour", HeaderText = "Food balance\n k / Hour" });
            dgv1.Columns.Add(new DataGridViewTextBoxColumn { Name = "dgvcFoodBalancePerHour", DataPropertyName = "IronPerHour", HeaderText = "Iron balance\n k / Hour" });
            dgv1.Columns.Add(new DataGridViewTextBoxColumn { Name = "dgvcFoodBalancePerHour", DataPropertyName = "WoodPerHour", HeaderText = "Wood balance\n k / Hour" });
            dgv1.Columns.Add(new DataGridViewTextBoxColumn { Name = "dgvcFoodBalancePerHour", DataPropertyName = "StonePerHour", HeaderText = "Stone balance\n k / Hour" });
            dgv1.Columns.Add(new DataGridViewTextBoxColumn { Name = "dgvcFoodBalancePerHour", DataPropertyName = "GoldPerHour", HeaderText = "Gold balance\n k / Hour" });
        }



        private void button1_Click(object sender, EventArgs e)
        {
            UnitType u1 = (UnitType)cbUnitType1.SelectedValue;
            UnitType u2 = (UnitType)cbUnitType2.SelectedValue;

            dgv1.Columns["dgvcRatioTroop1"].HeaderText = "% " + u1.ToString();

            IEnumerable<RatioGain> res =
                Compute.GainPerTS(
                    u1, int.Parse(tbResearch1.Text),
                    u2, int.Parse(tbResearch2.Text, CultureInfo.InvariantCulture),
                    double.Parse(tbRoundtripTime.Text, CultureInfo.InvariantCulture), (DungeonType)cbDungeonType.SelectedValue, 0.60,
                    ckbForceBuild.Checked ? (MilitaryBuild)cbBuilds.SelectedValue : null);
            int ratioMax = res.Where(a => a.TotalWithoutGoldPerHour == res.Max(b => b.TotalWithoutGoldPerHour)).First().Ratio;
            dgv1.DataSource = ExtractRatioGain(res, 0, ratioMax, 100, 50, 25, 75, 33, 66).ToArray();
            foreach (DataGridViewRow row in dgv1.Rows)
            {
                if ((row.DataBoundItem as RatioGain).Ratio == ratioMax)
                {
                    foreach (DataGridViewCell cell in row.Cells)
                    {
                        cell.Style.Font = new Font(cell.InheritedStyle.Font, FontStyle.Bold);
                    }
                }
            }
        }

        private IEnumerable<RatioGain> ExtractRatioGain(IEnumerable<RatioGain> source, params int[] ratios)
        {
            return source.Where(a => ratios.Contains(a.Ratio));
        }

        private void cbUnitType_SelectedIndexChanged(object sender, EventArgs e)
        {
            UpdateBuildCombo();
        }

        private void UpdateBuildCombo()
        {
            UnitType u1 = (UnitType)cbUnitType1.SelectedValue;
            UnitType u2 = (UnitType)cbUnitType2.SelectedValue;
            cbBuilds.DataSource = Data.CastleBuilds.Where(b => b.AvailableUnits.Contains(u1) || b.AvailableUnits.Contains(u2)).ToList();
        }

        private void cbUnitType1_SelectedIndexChanged(object sender, EventArgs e)
        {
            UpdateUnit2Combo();
        }

        private void UpdateUnit2Combo()
        {
            UnitType u1 = (UnitType)cbUnitType1.SelectedValue;
            cbUnitType2.DataSource = Data.CastleBuilds.Where(b => b.AvailableUnits.Contains(u1)).SelectMany(b => b.AvailableUnits).Distinct().ToList();
            cbUnitType2.SelectedIndex = cbUnitType2.Items.Count > 1 ? 2 : 0;
        }
    }
}
